package com.lemon.smartsafetynew.ui.adapter.patrol;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolGetTaskBo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.Date;
import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/7/31.
 */

public class PatrolTaskListAdapter extends LemonRcvAdapter<SafePatrolTaskVo> {

    private RecyclerTClickListener<SafePatrolTaskVo> mListener;

    public PatrolTaskListAdapter(@Nullable List<SafePatrolTaskVo> data,
                                    RecyclerTClickListener<SafePatrolTaskVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new SafePatrolTaskItem();
    }

    private class SafePatrolTaskItem implements AdapterItem<SafePatrolTaskVo> {

        private SafePatrolTaskVo mData;
        private int mPosition;

        private TextView safePatrolTaskNameTv;
        private TextView safePatrolWorkerTv;
        private TextView safePatrolTimeTv;
        private LinearLayout safePatrolTaskLayout;
        private ImageView safePatrolStatusImageView;

        @Override
        public int getLayoutResId() {
            return R.layout.item_safe_patrol_task;
        }

        @Override
        public void bindViews(View root) {

            safePatrolTaskLayout = (LinearLayout)root.findViewById(R.id.safe_patrol_task_layout);
            safePatrolTimeTv = (TextView)root.findViewById(R.id.safe_patrol_time_tv);
            safePatrolWorkerTv = (TextView)root.findViewById(R.id.safe_patrol_worker_tv);
            safePatrolTaskNameTv = (TextView)root.findViewById(R.id.safe_patrol_task_name_tv);
            safePatrolStatusImageView = (ImageView)root.findViewById(R.id.safe_patrol_status_imageView);

        }

        @Override
        public void setViews() {
            safePatrolTaskLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);
                }
            });
        }

        @Override
        public void handleData(SafePatrolTaskVo safePatrolTaskVo, int position) {

            mData = safePatrolTaskVo;
            mPosition = position;

            safePatrolTaskNameTv.setText(safePatrolTaskVo.getF_name());
            safePatrolWorkerTv.setText(safePatrolTaskVo.getUser_name());
            safePatrolTimeTv.setText(CommonUtil.timeToStr(safePatrolTaskVo.getPlan_start_time())
                    + " ~ " + CommonUtil.timeToStr(safePatrolTaskVo.getPlan_end_time()));

            if (SafePatrolGetTaskBo.State.to_do.getValue().equals(safePatrolTaskVo.getF_state())) {
                safePatrolStatusImageView.setImageResource(R.mipmap.icon_pratrol_to_do);
            } else if(SafePatrolGetTaskBo.State.doing.getValue().equals(safePatrolTaskVo.getF_state())) {
                safePatrolStatusImageView.setImageResource(R.mipmap.icon_pratrol_doing);
            } else if(SafePatrolGetTaskBo.State.done.getValue().equals(safePatrolTaskVo.getF_state())) {
                safePatrolStatusImageView.setImageResource(R.mipmap.icon_pratrol_done);
            } else if(SafePatrolGetTaskBo.State.over_time.getValue().equals(safePatrolTaskVo.getF_state())) {
                safePatrolStatusImageView.setImageResource(R.mipmap.icon_pratrol_delay);
            }

        }
    }

}
